package com.leaderhr.fishshop.controller.product;

import java.util.List;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;






import com.leaderhr.fishshop.form.Json;
import com.leaderhr.fishshop.form.ProductTypeForm;
import com.leaderhr.fishshop.form.Tree;
import com.leaderhr.fishshop.model.product.ProductType;
import com.leaderhr.fishshop.service.product.IProductTypeService;

@Controller
@RequestMapping("/admin/product")
public class ProductTypeController {

	private IProductTypeService productTypeService;

	public IProductTypeService getProductTypeService() {
		return productTypeService;
	}
	@Inject
	public void setProductTypeService(IProductTypeService productTypeService) {
		this.productTypeService = productTypeService;
	}
	/**
	 * 获得商品类别列表
	 * @return
	 */
	@RequestMapping("/productTypes")
	@ResponseBody
	public List<ProductTypeForm> treeGrid() {
		
		return productTypeService.treeGrid();
	}
	/**
	 * 商品类别树
	 * 
	 * @return
	 */
	@RequestMapping("/tree")
	@ResponseBody
	public List<Tree> tree() {
		return productTypeService.tree();
	}

	/**
	 * 跳转到类别添加页面
	 * 
	 * @return
	 */
	@RequestMapping("/toAddType")
	public String add(Model model) {
		ProductTypeForm productTypeForm = new ProductTypeForm();
		model.addAttribute("productType", productTypeForm);
		return "/admin/product/productTypeAdd";
	}
	/**
	 * 添加类别
	 * 
	 * @return
	 */
	@RequestMapping("/doAddType")
	@ResponseBody
	public Json add(ProductTypeForm productTypeForm) {
		Json j = new Json();
		productTypeService.add(productTypeForm);
		j.setSuccess(true);
		j.setMsg("添加成功！");
		return j;
	}
	/**
	 * 跳转到商品类别修改页面
	 * 
	 * @return
	 */
	@RequestMapping("/toEditType")
	public String editPage(Model model, Integer id) {
		ProductTypeForm ptf = productTypeService.load(id);
		model.addAttribute("productTypeForm", ptf);
		return "/admin/product/productTypeEdit";
	}

	/**
	 * 修改商品类别
	 * 
	 * @param role
	 * @return
	 */
	@RequestMapping("/doEditType")
	@ResponseBody
	public Json edit(ProductTypeForm productTypeForm) {
		Json j = new Json();
		productTypeService.edit(productTypeForm);
		j.setSuccess(true);
		j.setMsg("编辑成功！");
		return j;
	}


}
